﻿#region LicenseHeader

// Copyright 2012 The Trustees of Indiana University.  All rights reserved.
// 
// The Indiana University licenses this file to you under the Apache License, 
// Version 2.0 (the "License"); you may not use this file except in compliance 
// with the License.  You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// @author Thilina Gunarathne (tgunarat@indiana.edu)

#endregion

#region

using System;
using System.Collections.Generic;

#endregion

namespace AzureMRCore.DataTypes
{
    public abstract class Key
    {
        public abstract uint GetHashValue();
        public abstract Boolean Parse(object key);
        public abstract string GetTextValue();

        public override string ToString()
        {
            return GetTextValue();
        }
    }

    public class KeyComparer : IEqualityComparer<Key>
    {
        #region IEqualityComparer<Key> Members

        public bool Equals(Key x, Key y)
        {
            if (x.GetTextValue().Equals(y.GetTextValue()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public int GetHashCode(Key obj)
        {
            return (int) obj.GetHashValue();
        }

        #endregion
    }

    /// <summary>
    ///     To be used as a dummy when we need an optional Type argument Eg: Mapper with no broadcast input.
    /// </summary>
    public class NullKey : Key
    {
        public override uint GetHashValue()
        {
            return 0;
        }

        public override bool Parse(object key)
        {
            return true;
        }

        public override string GetTextValue()
        {
            return null;
        }
    }
}